﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SMTC.Dao.Plan;
using SMTC.Model.Domain;
using System.Data.Common;
using USPCommon.Core.Log;

namespace SMTC.BO.Plan
{
    public class PlanProccessServices
    {
        private PlanProcessDao _dao = new PlanProcessDao();

        /// <summary>
        /// 插入计划信息
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public bool InsertPlan(List<ImportPlanDomain> list, string versionRemark = null,PlanVersionDomain version = null)
        {
            DbTransaction dbt = null;
            PlanVersionDomain newVersion = new PlanVersionDomain();
            if (version == null)
            {
                if (versionRemark == null)
                {
                    newVersion = this.GetNewPlanVersion();
                }
                else
                {
                    newVersion = this.GetNewPlanVersion(versionRemark);
                }
            }
            else
            {
                newVersion = version;
            }
            try
            {
                foreach (var item in list)
                {
                    item.PlanVersion = newVersion.PlanVersion;
                    if (string.IsNullOrEmpty(item.BU) || item.BU.Length > 20 || item.BU.Contains("'Business Unit (Current)")) continue;
                    if (!_dao.InsertPlanHeader(item, ref dbt))
                    {
                        throw new Exception("insert plan failer");
                    }

                    for (int index = 1; index <= 12; index++)
                    {
                        try
                        {
                            if (!_dao.InsertPlanDetail(item, index, ref dbt))
                            {
                                throw new Exception("insert planDetail failer");
                            }
                        }
                        catch
                        {
                            if (dbt != null) dbt.Rollback();
                            return false;
                        }
                    }
                }
                dbt.Commit();
                return true;
            }
            catch (Exception ex)
            {
                PLLog.Debug("插入计划信息失败", ex);
                if (dbt != null) dbt.Rollback();
                return false;
            }
        }
        /// <summary>
        /// 删除计划信息
        /// </summary>
        /// <param name="planID"></param>
        /// <returns></returns>
        public bool DeletePlan(string planID)
        {
            DbTransaction dbt = null;
            bool operResult = true;
            try
            {
                operResult = operResult && _dao.DeletePlanHeader(planID, ref dbt);
                operResult = operResult && _dao.DeletePlanDetail(planID, ref dbt);

                if (operResult == true)
                {
                    dbt.Commit();
                    return true;
                }
                else
                {
                    if (dbt != null) dbt.Rollback();
                    return false;
                }
            }
            catch
            {
                if (dbt != null) dbt.Rollback();
                return false;
            }
        }
        /// <summary>
        /// 更新计划信息
        /// </summary>
        /// <param name="domain"></param>
        /// <returns></returns>
        public bool UpdatePlan(ImportPlanDomain domain)
        {
            DbTransaction dbt = null;
            bool operReusult = true;

            try
            {
                operReusult = operReusult && _dao.UpdatePlanHeader(domain, ref dbt);
                for (int month = 1; month <= 12; month++)
                {
                    operReusult = operReusult && _dao.UpdatePlanDetail(domain, month, ref dbt);
                }
                if (operReusult == false)
                {
                    if (dbt != null) dbt.Rollback();
                    return false;
                }
                else
                {
                    if (dbt != null) dbt.Commit();
                    return true;
                }
            }
            catch
            {
                if (dbt != null) dbt.Rollback();
                return false;
            }
        }

        public bool CopyVersion(string OldVerson, string reMark = "")
        {
            PlanVersionDomain newVersion = this.GetNewPlanVersion(reMark);
            return _dao.CopyVersion(OldVerson, newVersion.PlanVersion);
        }

        public PlanVersionDomain GetMaxPlanVersion()
        {
            List<PlanVersionDomain> list = _dao.GetPlanVersionListDesc();
            if (list != null && list.Count > 0)
            {
                return list[0];
            }
            else
            {
                PlanVersionDomain version = new PlanVersionDomain();
                version.VersionMonth = DateTime.Now.ToString("yyyyMM");
                version.VersionIndex = "1";
                version.VersionRemark = "UpLoad";
                return version;
            }
        }
        public PlanVersionDomain GetNewPlanVersion(string Remark = "")
        {
            PlanVersionDomain maxVersion = this.GetMaxPlanVersion();
            PlanVersionDomain newVersion = new PlanVersionDomain();
            if (maxVersion.VersionMonth == DateTime.Now.ToString("yyyyMM"))
            {
                newVersion.VersionMonth = maxVersion.VersionMonth;
                newVersion.VersionIndex = (int.Parse(maxVersion.VersionIndex) + 1).ToString();
                newVersion.VersionRemark = DateTime.Now.ToString("yyyyMMddHHmmss") + "Excel上传";
            }
            else
            {
                newVersion.VersionMonth = DateTime.Now.ToString("yyyyMM");
                newVersion.VersionIndex = "1";
                newVersion.VersionRemark = DateTime.Now.ToString("yyyyMMddHHmmss") + "Excel上传";
            }

            if (!string.IsNullOrEmpty(Remark))
            {
                newVersion.VersionRemark = Remark;
            }
            return newVersion;
        }

        public bool DeletePlanByVersion(string PlanVersion)
        {
            DbTransaction dbt = null;
            bool result = true;
            try
            {
                result &= _dao.DeletePlanDetailByVersion(PlanVersion, ref dbt);
                result &= _dao.DeletePlanByVersion(PlanVersion, ref dbt);
                if (result == true)
                {
                    dbt.Commit();
                    return true;
                }
                else
                {
                    throw new Exception("删除计划：1明细，2头信息步骤中失败");
                }
            }
            catch (Exception ex)
            {
                PLLog.Error("删除计划版本出错", ex);
                dbt.Rollback();
                return false;
            }
        }
    }
}
